package com.tanhua.dubbo.utils;

import com.tan.model.mongo.Friend;
import com.tan.model.mongo.Movement;
import com.tan.model.mongo.MovementTimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TimeLinerService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Async
    public void saveTimeLine(Long userId, Movement movement) {
        //2.查询当前用户的好友数据
        Query query = Query.query(Criteria.where("userId").is(movement.getUserId()));
        List<Friend> list = mongoTemplate.find(query, Friend.class);
        //3.循环好友数据，构建时间线数据存入数据库
        for (Friend friend : list) {
            MovementTimeLine movementTimeLine = new MovementTimeLine();
            movementTimeLine.setUserId(friend.getUserId());
            movementTimeLine.setFriendId(friend.getFriendId());
            movementTimeLine.setCreated(System.currentTimeMillis());
            movementTimeLine.setMovementId(movement.getId());
            mongoTemplate.save(movementTimeLine);

        }
    }

}
